package com.thssh.ui.lsn04.view;

import android.animation.ValueAnimator;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.LinearGradient;
import android.graphics.Matrix;
import android.graphics.Paint;
import android.graphics.Shader;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;

/**
 * @author zhangyugehu
 * @version V1.0
 * @date 2017/09/14
 * email: zhangyugehu@163.com
 */

public class LinearGradientTextView extends View {

    private Paint mTextPaint;
    private Paint mShaderPaint;
    private String mText;

    private LinearGradient mLinearGradient;
    private ValueAnimator mShaderAnimator;
    private float mTextWidth;
    private float mShaderWidth;
    private float mDx;
    private Matrix mShaderMatrix;


    public LinearGradientTextView(Context context) {
        this(context, null);
    }

    public LinearGradientTextView(Context context, @Nullable AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public LinearGradientTextView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        mTextPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
        mShaderPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
        mTextPaint.setColor(Color.BLACK);
        mText = "LinearGradientTextView";
        mTextPaint.setTextSize(80);
        mShaderPaint.setTextSize(80);
        mTextWidth = mShaderPaint.measureText(mText);
        mShaderWidth = mTextWidth / 3;
        mShaderMatrix = new Matrix();
//        int [] colors = new int[]{0x000000, 0x2200000, 0x000000};
        int [] colors = new int[]{Color.WHITE, Color.BLACK, Color.WHITE};
        mLinearGradient = new LinearGradient(0, 0, mShaderWidth, 0, colors, null, Shader.TileMode.CLAMP);
//        mTextPaint.setShader(mLinearGradient);
        mShaderPaint.setShader(mLinearGradient);

        mShaderAnimator = ValueAnimator.ofFloat(0, 1);
        mShaderAnimator.setRepeatCount(ValueAnimator.INFINITE);
        mShaderAnimator.setRepeatMode(ValueAnimator.REVERSE);
        mShaderAnimator.setDuration(3000);
        mShaderAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
            @Override
            public void onAnimationUpdate(ValueAnimator animation) {
                float value = (float) animation.getAnimatedValue();
                mDx = mTextWidth * value - mShaderWidth * 2  / 3;
                invalidate();
            }
        });
        mShaderAnimator.start();
    }

    @Override
    protected void onDetachedFromWindow() {
        super.onDetachedFromWindow();
        if(mShaderAnimator != null){
            mShaderAnimator.cancel();
        }
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);

        mShaderMatrix.setTranslate(mDx, 0);
        mLinearGradient.setLocalMatrix(mShaderMatrix);
        canvas.drawText(mText, 0, 100, mTextPaint);
        canvas.drawText(mText, 0, 100, mShaderPaint);
//        canvas.drawRect(0,0,mShaderWidth,200,mShaderPaint);
    }
}
